#include "quantum.h"
#include "components/view.h"
#include "event/event.h"
#include "event/event_poll.h"
#include "utils/ncurses_utils.h"
#include "window.h"
#include <assert.h>
#include <bg_helper/to_string.hpp>
#include <cstdlib>
#include <spdlog/spdlog.h>

Quantum::Quantum(ArgConfig &config) : _config(std::move(config)) {
	curses_init();
	/* mousemask(ALL_MOUSE_EVENTS, NULL); */
	/* scrollok(stdscr, true); */
}

int Quantum::run() {
	std::shared_ptr<Window> w = std::make_shared<Window>();
	// w->create_view(LayoutMode::Horizontal, true);

	// force update
	w->update_all();

	while (w->get_status() != WindowStatus::EXIT) {
		EventPoll::pull_event();
		Event e;
		do {
			e = EventPoll::get_event();

			// process event
			switch (e.event) {
			case EventType::KeyDown:
				if (e.key) {
					w->key_input_event(e.key);
				}
				w->update();
				break;
			case EventType::MouseMove:
				spdlog::info("Mouse moved! POS: X: {} Y: {}", e.mouse.x,
							 e.mouse.y);
				break;
			case EventType::Resize:
				spdlog::info("Terminal resized!");
				w->update_all();
				break;
			default:
				break;
			}

		} while (e.event != EventType::None);
	}

	spdlog::info("Window exits!");

	return 0;
}

Quantum::~Quantum() { endwin(); }
